import { Mesh, MeshBasicMaterial, EdgesGeometry, LineBasicMaterial, LineSegments, ExtrudeGeometry, Shape } from "three";
import { Object3D } from "three/src/core/Object3D";
import { Point } from "../../Engine";

/**
 * 根据顶点数组拉伸出一个带有边框的建筑
 */
export default class Building extends Object3D {
    private buildingData: {pointArray: Array<Point>, solidHeight: number, solidColor: string, solidAlpha: number, hasBorder?: boolean, isEmptyBuilding?:boolean, lineColor: string, lineApha: number};

    /**
     * 根据顶点数组拉伸出一个带有边框的建筑
     * @param buildingData 创建建筑需要的数据
     * @param buildingData.pointArray 顶点数组
     * @param buildingData.solidHeight 建筑的高度
     * @param buildingData.solidColor 建筑的颜色
     * @param buildingData.solidAlpha 建筑的透明度
     * @param buildingData.hasBorder 是否带建筑边框，true表示带边框，false表示不带边框。默认值为false
     * @param buildingData.isEmptyBuilding 该建筑是否只有边框，true表示没有实体建筑，false表示没有实体建筑。默认值为false
     * @param buildingData.lineColor 边框线的颜色
     * @param buildingData.lineApha 边框线的透明度。默认值为1
     * @example  let pointArray = [ //3857表示web墨卡托坐标系，4326表示经纬度坐标
            new GE.Point(13376713.90129997, 3540579.2959903134, 0, '3857'),
            new GE.Point(13376711.98134067, 3539826.5375406537, 0, '3857'),
            new GE.Point(13377952.49603797, 3539759.3270052206, 0, '3857'),
            new GE.Point(13378008.185227688, 3540675.3111583497, 0, '3857'),
            new GE.Point(13377184.375437068, 3540339.2581293588, 0, '3857')
        ];

        let buildingData = {
            pointArray: pointArray,  //多边形的顶点数组
            solidHeight: 100, //建筑的高度
            solidColor: '#0000FF', //建筑的颜色
            solidAlpha: 0.5,  //建筑的透明值
            hasBorder: true, //建筑是否带边框, 默认值为false
            isEmptyBuilding: false, //是否不显示实体建筑，默认值为false
            lineColor: '#DC143C', //边框线的颜色
            lineApha: 1, //边框线的透明度, 默认值是1
        };

        let building = new GE.Building(buildingData); 
        app.add(building);
     */
    public constructor(buildingData: {pointArray: Array<Point>, solidHeight: number, solidColor: string, solidAlpha: number, hasBorder?: boolean, isEmptyBuilding?:boolean, lineColor: string, lineApha: number}){
        super();
        this.buildingData = buildingData;

        this.createBuilding();
    }

    /**
     * 创建建筑
     * @param hasBorder 是否带建筑边框，true表示带边框，false表示不带边框。默认值为false
     * @param isEmptyBuilding 该建筑是否只有边框，true表示没有实体建筑，false表示没有实体建筑。默认值为false
     */
    private createBuilding(){
        let centerPoint = this.buildingData.pointArray[0].toEPSGWeb();
        let shape = new Shape();
        this.buildingData.pointArray.forEach((currentPoint, i) => {
            currentPoint = currentPoint.toEPSGWeb().clone().sub(centerPoint);
            i == 0 ? shape.moveTo(currentPoint.x, currentPoint.y) : shape.lineTo(currentPoint.x, currentPoint.y);
        });

        let extrudeSettings = {
            depth: this.buildingData.solidHeight,
        };
        let geometry = new ExtrudeGeometry(shape, extrudeSettings);
       
        if(!this.buildingData.isEmptyBuilding){
            let material = new MeshBasicMaterial({
                transparent: true,
                opacity: this.buildingData.solidAlpha,
                color: this.buildingData.solidColor
            },);
            let mesh = new Mesh(geometry, material);
            mesh.position.copy(centerPoint.clone().sub(this.position));
            this.add(mesh);
        }

        if(this.buildingData.hasBorder){
            const edges = new EdgesGeometry( geometry ); 
            let lineApha = (this.buildingData.lineApha == undefined) ? 1 : this.buildingData.lineApha;
            const line = new LineSegments(edges, new LineBasicMaterial( { color: this.buildingData.lineColor, transparent: true, opacity: lineApha} ) ); 
            line.position.copy(centerPoint.clone().sub(this.position));
            this.add(line);
        }
    }

}